/**
 * 开始界面
 */

import AudioManager from "./SdkTools/AudioManager";
import Define, { CustomAdType } from "./SdkTools/Define";
import gameCheckLogin from "./SdkTools/gameCheckLogin";
import gamePrefabMgr, { uiPreName } from "./SdkTools/gamePrefabMgr";
import gameShareAdsVideo from "./SdkTools/gameShareAdsVideo";
import gameStorage from "./SdkTools/gameStorage";
import PlayData, { GameType } from "./SdkTools/PlayData";
import storage from "./SdkTools/storage";

const { ccclass, property } = cc._decorator;

@ccclass
export default class StartScene extends cc.Component {

    @property(cc.Node)
    top: cc.Node = null

    @property(cc.Node)
    soundbtn: cc.Node = null

    @property(cc.Node)
    subContextNode: cc.Node = null;

    onLoad() {
        AudioManager.setSoundEvent()

        if (storage.Instance.isfirstlogin == !0) {
            storage.Instance.getUserInfo()
            this.setOffTimeTiLi()
        }

        //console.log('打印终端信息', tt.getSystemInfoSync())

        if (cc.sys.platform == cc.sys.BYTEDANCE_GAME) {
            // cc.find('Canvas/bg/left/rank').active = !1
            //@ts-ignore
            // const sysinfo = tt.getSystemInfoSync()
            // const sdkversion = sysinfo.SDKVersion
            // let appname = sysinfo.appName
            // let appVersion = sysinfo.version
            // if (Utils.compareVersion(sdkversion, '2.70.0') >= 0
            //     && (appname == 'Douyin' || appname == 'douyin_lite')
            //     && Utils.compareVersion(appVersion, '23.2.0') >= 0) {
            //         cc.find('bg/right/rank', this.node).active = !0
            // }else{
            //     cc.find('bg/left/rank', this.node).active = !1
            // }
        } else if (cc.sys.platform == cc.sys.WECHAT_GAME) {
            //@ts-ignore
            // let env = window.wx
            // if (env) {
            //     console.log('Message posted');
            //     env.getOpenDataContext().postMessage({
            //         value: 'MESSAGE FROM MAIN PROJECT',
            //     });
            // }

            //cc.find('bg/left/rank', this.node).active = !1
        }

        else {
            // cc.find('bg/left/rank', this.node).active = !1
        }

        //放最后
        storage.Instance.isfirstlogin = !1
    }

    start() {
        if (gameStorage.getMusic() == 1) {
            cc.find('on', this.soundbtn).active = !1
            cc.find('off', this.soundbtn).active = !0
        } else {
            cc.find('on', this.soundbtn).active = !0
            cc.find('off', this.soundbtn).active = !1
        }
        cc.director.emit('main_bg')

        gameShareAdsVideo.Instance.showCustomAd(CustomAdType.Shu)
    }

    /**
     * 设置离线体力
     */
    setOffTimeTiLi() {
        if (gameStorage.getoffLineTime() > 0) {
            let nowtime = Number(new Date().getTime())
            let offtime = gameStorage.getoffLineTime()
            let _chatime = Math.floor((nowtime - offtime) / 1000)
            let addtili = Math.floor(_chatime / Define.eneryTime)
            if (storage.Instance.getEnergy() < Define.maxEnery) {
                if ((addtili + storage.Instance.getEnergy()) >= Define.maxEnery) {
                    storage.Instance.setEnergy(Define.maxEnery)
                    cc.director.emit('addengry')
                } else {
                    storage.Instance.setEnergy(addtili + storage.Instance.getEnergy())
                    cc.director.emit('addengry')
                }
            }
        }
        gameStorage.setoffLineTime()
    }

    /**
     * 开始按钮
     */
    startBtnClick(t, e) {
        if (e == 'huaijiu') {
            PlayData.Instance.gameType = GameType.finddifference_zhaobutong
        }
        gameShareAdsVideo.Instance.hideCustomAd(CustomAdType.Shu)
        let newnode = cc.instantiate(gamePrefabMgr.Instance.uiPre[uiPreName.checkoutPanel])
        newnode.parent = cc.director.getScene()
    }

    /**
     * 音效开关
     */
    sountBtn() {
        if (gameStorage.getMusic() == 1) {
            gameStorage.setMusic(0)
            cc.director.emit('main_bg')
            cc.find('on', this.soundbtn).active = !0
            cc.find('off', this.soundbtn).active = !1
            gameStorage.setSound(0)
        } else {
            gameStorage.setMusic(1)
            cc.audioEngine.stopMusic()
            cc.find('on', this.soundbtn).active = !1
            cc.find('off', this.soundbtn).active = !0
            gameStorage.setSound(1)
        }
    }

    /**
     * 排行榜按钮点击
     */
    rankClick() {
        //gameShareAdsVideo.Instance.getImRankList_Num()
        //this.subContextNode.active = !0
        this.subContextNode.x = 0

        var self = this
        if (cc.sys.platform == cc.sys.WECHAT_GAME) {
            if (gameCheckLogin.gameCode) {
                console.log('开启排行')
                gameCheckLogin.wx_getUserInfo(function () {
                    self.openPaiHangPanel()
                })
            } else {
                //@ts-ignore
                wx.showModal({
                    title: "提示！",
                    content: "请先登录？",
                    success(res) {
                        if (res.confirm) {
                            gameCheckLogin.gameLogin(true)
                        } else if (res.cancel) {
                            //console.log("cancel, cold");
                        } else {
                            // what happend?
                        }
                    },
                    fail(res) {
                        //console.log(`showModal调用失败`);
                    },
                });
            }
        }
    }

    /**
     * 打开排行榜界面
     */
    openPaiHangPanel() {
        gameShareAdsVideo.Instance.chaPingAds_Show()
        //gameShareAdsVideo.Instance.bannerAds_Hide()
        // this.rankpanel.active = !0
        // this.rankpanel.x = 0
        // console.log('打印数据域节点=', cc.find('kuang/sub', this.rankpanel))
        // let sub = cc.find('kuang/sub', this.rankpanel).getComponent(cc.SubContextView)
        // sub.enabled = !0
        //sub.updateSubContextViewport()


        this.subContextNode.active = !1




        //@ts-ignore
        // let env = window.wx
        // if (env) {
        //     console.log('Message posted');
        //     env.getOpenDataContext().postMessage({
        //         value: 'MESSAGE FROM MAIN PROJECT',
        //     });
        // }



        //@ts-ignore
        let openDataContext = wx.getOpenDataContext()
        console.log('向子域发消息')
        openDataContext.postMessage({
            event: 'getRank',
            name: PlayData.Instance.userInfo.name,
            icon: PlayData.Instance.userInfo.icon
        })
    }

    closeRankPanel() {
        this.subContextNode.active = !1
    }

    /**
     * 排行榜邀请好友
     */
    shareGame() {
        gameShareAdsVideo.Instance.recording_Video_Share(() => { }, () => { })
    }

    // update (dt) {}
}
